package cn.dai.pojo2;

import cn.dai.util.JdbcDbcpUtil;
import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URLDecoder;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

/**
 * @author ArkD42
 * @file Jdbc
 * @create 2020 - 04 - 24 - 21:58
 */
public class DruidUtils {

    private static DataSource dataSource = null;

    static {
        String path = DruidUtils.class.getClassLoader().getResource("druid.properties").getFile();
        System.out.println(path);
        try {
            String decode = URLDecoder.decode(path, "utf-8");
            InputStream inputStream = new FileInputStream(decode);
            Properties properties = new Properties();
            properties.load(inputStream);
            dataSource = DruidDataSourceFactory.createDataSource(properties);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static Connection getConnection(){
        try {
            return dataSource.getConnection();
        } catch (SQLException sqlException) {
            sqlException.printStackTrace();
        }
        return null;
    }
}
